home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /***************************************************************************
- *
- * @(#) - BZLOGO - Convert an image to a logo for use in BZ.
- *
- * Chris Fouts - Silicon Graphics, Inc.
- * February, 1992
- **************************************************************************/
- #include <stdio.h>
- #include <fcntl.h>
- #include <string.h>
- #include <image.h>
- #include <vect.h>
- #include <objfnt.h>
- #include <sgiobj.h>
- #include <obj.h>
- #include <stream.h>
- #include <mesh.h>
-
- #define SCALE 100.0
-
- #define BZLOGO_MAGIC 0x425a4c00
-
- #define X(x) (int)( ( (x - xmin) * SCALE ) )
- #define Y(y) (int)( ( (y - ymin) * SCALE ) )
-
- #define MIN(a,b) ( ( (a) < (b) ) ? (a) : (b) )
- #define MAX(a,b) ( ( (a) > (b) ) ? (a) : (b) )
-
- static char *base_name ;
-
- extern short *shortimagedata() ;
- extern sgiobj *getfaces() ;
-
- static FILE *bzl_f ;
-
- static sgiobj *meshchar(
- short *sptr
- )
- {
- float x ;
- float y ;
- vect p0 ;
- vect p1 ;
- vect s ;
- vect l ;
- vect c ;
- int nverts ;
- int npnts ;
-
- tempbegin() ;
- p0.x = 0.0 ;
- p0.y = 0.0 ;
- p1.x = 1.0 ;
- p1.y = 0.0 ;
- tempsegment( &p0, 0, &p1, 0 ) ;
-
- pathbegin() ;
- while( 1 ) {
- switch(*sptr++) {
- case PO_BGNLOOP :
- npnts = 0 ;
- break ;
- case PO_ENDBGNLOOP :
- pathsegment( &l, 0, &s, 0 ) ;
- pathclose() ;
- npnts = 0 ;
- break ;
- case PO_RETENDLOOP :
- pathsegment( &l, 0, &s, 0 ) ;
- pathclose() ;
- return getfaces() ;
- break ;
- case PO_RET :
- return 0 ;
- break ;
- }
- nverts = *sptr++ ;
- while( nverts-- ) {
- if( npnts++ == 0 ) {
- l.x = s.x = sptr[0] ;
- l.y = s.y = sptr[1] ;
- }
- else {
- c.x = sptr[0] ;
- c.y = sptr[1] ;
- pathsegment( &l, 0, &c, 0 ) ;
- l = c ;
- }
- sptr+=2 ;
- }
- }
- }
-
-
-
- static int obj_to_short(
- object *path,
- short *chardata,
- int len
- )
- {
- float xmin ;
- float ymin ;
- float xmax ;
- float ymax ;
- int width ;
- int nshorts ;
- int pcnt ;
- object *pobj ;
- point *apnt ;
-
- xmin = ymin = 1E10;
- xmax = ymax = -1E10;
-
- pobj = path;
-
- while( pobj ) {
- if( ( apnt = pobj->points ) != NULL ) {
- while( apnt ) {
- xmin = MIN( xmin, apnt->x ) ;
- ymin = MIN( ymin, apnt->y ) ;
- xmax = MAX( xmax, apnt->x ) ;
- ymax = MAX( ymax, apnt->y ) ;
- apnt = apnt->next ;
- }
- }
- pobj = pobj->next ;
- }
-
- width = SCALE * ( xmax - xmin ) ;
- ymin = 0.3333 ;
-
- nshorts = 0 ;
- pobj = path ;
- while( pobj ) {
- if( ( apnt = pobj->points ) != NULL ) {
- if( nshorts == 0 )
- chardata[nshorts++] = PO_BGNLOOP ;
- else
- chardata[nshorts++] = PO_ENDBGNLOOP ;
- pcnt = 0 ;
- while( apnt ) {
- pcnt++ ;
- apnt = apnt->next ;
- }
- chardata[nshorts++] = pcnt ;
- apnt = pobj->points ;
- while( apnt ) {
- chardata[nshorts++] = X(apnt->x) ;
- chardata[nshorts++] = Y(apnt->y) ;
- apnt = apnt->next ;
- }
- }
- pobj = pobj->next;
- }
- chardata[nshorts++] = PO_RETENDLOOP ;
-
- return( nshorts ) ;
- }
-
-
-
- sgiobj *cvttotmesh(
- sgiobj *obj
- )
- {
- sgiobj *robj ;
-
- robj = tmeshobj( obj ) ;
- freesgiobj( obj ) ;
-
- return( robj ) ;
- }
-
-
-
- static void outcmd(
- char *cmd
- )
- {
- fputs( cmd, bzl_f ) ;
- fputc( '\n', bzl_f ) ;
- }
-
-
-
- static void outuchar(
- unsigned char val1
- )
- {
- fprintf( bzl_f, " %d\n", (int)val1 ) ;
- }
-
-
-
- static void outucharpair(
- unsigned char val1,
- unsigned char val2
- )
- {
- fprintf( bzl_f, " %d %d\n", (int)val1, (int)val2 ) ;
- }
-
-
-
- static int limit_mesh(
- sgiobj *obj,
- float *xmin,
- float *xmax,
- float *ymin,
- float *ymax
- )
- {
- long *data;
- char *vertdata ;
- char *avert ;
- int vertlongs ;
- int nverts ;
- float *fptr;
- int cnt = 0 ;
-
- *xmin = 1e30 ;
- *xmax = -1e30 ;
- *ymin = 1e30 ;
- *ymax = -1e30 ;
-
- data = obj->data ;
- vertlongs = *data++ ;
- vertdata = (char *)data ;
- data += vertlongs ;
- while( 1 ) {
- switch( *data++ ) {
- case OP_BGNTMESH :
- cnt++ ;
- break ;
- case OP_SWAPTMESH :
- cnt++ ;
- break;
- case OP_ENDBGNTMESH :
- cnt++ ;
- break ;
- case OP_ENDTMESH :
- cnt++ ;
- return( cnt ) ;
- break ;
- default :
- fprintf( stderr, "drawsgiobj: bad tmesh op %d\n", *data ) ;
- return( 0 ) ;
- break ;
- }
- nverts = *data++ ;
- cnt++ ;
- while( nverts-- ) {
- avert = vertdata + *data++ ;
- fptr = (float*)( avert + ( sizeof(long) * OFFSET_POINT ) ) ;
- if( fptr[0] < *xmin ) *xmin = fptr[0] ;
- if( fptr[1] < *ymin ) *ymin = fptr[1] ;
- if( fptr[0] > *xmax ) *xmax = fptr[0] ;
- if( fptr[1] > *ymax ) *ymax = fptr[1] ;
- cnt += 2 ;
- }
- }
- }
-
-
-
- static int save_mesh(
- sgiobj *obj
- )
- {
- long *data;
- char *vertdata ;
- char *avert ;
- int vertlongs ;
- int nverts ;
- float *fptr;
- int cnt ;
- float xmin ;
- float xmax ;
- float ymin ;
- float ymax ;
- float xmid ;
- float ymid ;
- float scale ;
-
- if( ( cnt = limit_mesh( obj, &xmin, &xmax, &ymin, &ymax ) ) == 0 ) {
- return( 0 ) ;
- }
-
- if( xmax == xmin && ymax == ymin ) {
- fprintf( stderr, "%s: no image picked out\n", basename ) ;
- exit( 1 ) ;
- }
-
- if( ymax - ymin > xmax - xmin ) {
- scale = 255.0 / ( ymax - ymin ) ;
- }
- else {
- scale = 255.0 / ( xmax - xmin ) ;
- }
- xmid = ( 255.0 - scale * ( xmax - xmin ) ) / 2. ;
- ymid = ( 255.0 - scale * ( ymax - ymin ) ) / 2. ;
-
- data = obj->data ;
- vertlongs = *data++ ;
- vertdata = (char *)data ;
- data += vertlongs ;
- while( 1 ) {
- switch( *data++ ) {
- case OP_BGNTMESH :
- outcmd( "BGNTMESH" ) ;
- break ;
- case OP_SWAPTMESH :
- outcmd( "SWAPTMESH" ) ;
- break;
- case OP_ENDBGNTMESH :
- outcmd( "ENDBGNTMESH" ) ;
- break ;
- case OP_ENDTMESH :
- outcmd( "RETENDTMESH" ) ;
- return( cnt ) ;
- break ;
- default :
- fprintf( stderr, "%s: bad tmesh op %d\n", basename, *data ) ;
- return( 0 ) ;
- break ;
- }
- nverts = *data++ ;
- outuchar( (unsigned char)nverts ) ;
- while( nverts-- ) {
- avert = vertdata + *data++ ;
- fptr = (float*)( avert + ( sizeof(long) * OFFSET_POINT ) ) ;
- outucharpair( (unsigned char)( scale * ( fptr[0]-xmin ) +xmid ) ,
- (unsigned char)( scale * ( fptr[1]-ymin ) +ymid ) ) ;
- }
- }
- }
-
-
-
- static void usage( void )
- {
- fprintf( stderr, "Usage: %s image out.bzl [-left | -right] [stepsize]\n",
- base_name ) ;
- exit( 1 ) ;
- }
-
-
-
- main(
- int argc,
- char **argv
- )
- {
- objfnt *fnt ;
- sgiobj *obj ;
- short *imgdata ;
- object *path ;
- int xsize ;
- int ysize ;
- float stepsize = 2.0 ;
- short chardata[10000] ;
- IMAGE *image ;
- long magic ;
- int n ;
- int side = 0 ;
-
- if( ( base_name = strrchr( argv[0], '/' ) ) == NULL ) {
- base_name = argv[0] ;
- }
- else {
- base_name++ ;
- }
-
- if( argc < 3 ) {
- usage() ;
- exit( 1 ) ;
- }
-
- if( ( bzl_f = fopen( argv[2], "w" ) ) == NULL ) {
- perror( argv[2] ) ;
- exit( 1 ) ;
- }
- fputs( "# BZLOGO Version 1.1\n", bzl_f ) ;
-
- n = 3 ;
- while( argc > n ) {
- if( strcmp( argv[n], "-left" ) == 0 ) {
- side |= 1 ;
- }
- else if( strcmp( argv[n], "-right" ) == 0 ) {
- side |= 2 ;
- }
- else {
- stepsize = atof( argv[n] ) ;
- if( stepsize < 0.1 )
- stepsize = 0.1 ;
- }
- n++ ;
- }
- printf( "stepsize = %f\n", stepsize ) ;
-
- switch( side ) {
- case 0 :
- break ;
- case 1 :
- fputs( "LEFTSIDE\n", bzl_f ) ;
- break ;
- case 2 :
- fputs( "RIGHTSIDE\n", bzl_f ) ;
- break ;
- default :
- usage() ;
- break ;
- }
-
- /*
- * Read in image and store in imgdata.
- */
- if( ( imgdata = (short *)shortimagedata( argv[1] ) ) == NULL ) {
- fprintf( stderr, "%s: could not open image `%s'\n", base_name,
- argv[1] ) ;
- exit( 1 ) ;
- }
-
- sizeofimage( argv[1], &xsize, &ysize ) ;
-
- path = fbtoshape( imgdata, xsize, ysize, 128,
- RESAMPLEFUNC|SMOOTHFUNC|FIXCORNERFUNC,stepsize);
- if( path == NULL ) {
- fprintf( stderr, "%s: image is either all white or all black\n",
- base_name ) ;
- exit( 1 ) ;
- }
-
- /*
- * Convert raster to outline format in chardata.
- */
- obj_to_short( path, chardata, 10000 ) ;
- freeobj( path );
-
- /*
- * Convert from outline to triangular mesh.
- */
- pathnoise( 0.000 ) ;
- if( ( obj = meshchar( chardata ) ) == NULL ) {
- fprintf( stderr, "%s: could not mesh the image.\n", base_name ) ;
- exit( 1 ) ;
- }
-
- obj = cvttotmesh( obj ) ;
-
- printf( "%d words\n", save_mesh( obj ) ) ;
-
- fclose( bzl_f ) ;
- return( 0 ) ;
- }
-
-
-
-